<?php
namespace Home\Controller;
use Think\Controller;
use Think\Page;
class SwitchController extends CommonController {
	public function transfer(){
		$this -> assign('title','注册积分转入');
		$mod = M('users');
		$user = $mod -> where('user_id = "%s"',$_SESSION['users']['user_id'])->find();
		$data = explode(',',$_SESSION['users']['tree_code']);
		$mod_relave = M('zczhuan');
		$relave = $mod_relave -> where('main_id = "%s" ',$_SESSION['users']['user_id']) ->select();
		$count = $mod_relave -> where('main_id = "%s" ',$_SESSION['users']['user_id'])->count();
		
			
		$this -> assign('user',$user);
		$this -> assign('relave',$relave);
		$this -> display('');
	}

	public function transdeleteAjax(){
		$mod = M('zczhuan');
		$res = $mod -> where('user_id = "%s"',I('post.user_delete_id')) ->delete();
		if($res>0){
			echo '1';
		}else{
			echo '2';
		}
		
	}
	public function switchAjax(){
		$mod = M('users');
		$res = $mod -> where('user_id='.I('post.out_user_id')) -> find();
		if(md5(I('post.repassword'))==$_SESSION['users']['repassword']){
			if($res>0){
				echo '1';
			}else{
				echo '22';
			}
		}else{
			echo '2';
		}
	}
	//处理注册积分转入
	public function dotransfer(){
		// dump(I('post.'));
		$mod = M('users');
		$log_mod = M('account_log');
		$mod -> startTrans();
		//判断是否是下面的给上面的人转
		$tree_code = explode(',',$_SESSION['users']['tree_code']);	
		$res = in_array(I('post.out_user_id'),$tree_code);
		//判断是否是上面的人给下面的转
		$tree_out_code = $mod -> where('user_id = "%s" ',I('post.out_user_id')) -> find()['tree_code'];
		$tree_out_code = explode(',',$tree_out_code);
		$res2 = in_array($_SESSION['users']['user_id'],$tree_out_code);
		if($res>0 || $res2>0){
			$outUser = $mod -> where('user_id = "%s" ',I('post.out_user_id'))->find();
			$outRes = M() -> execute('update zx_users set user_money2=user_money2+'.I('post.out_money').' where user_id='.I('post.out_user_id'));
			// $outUser['user_money2'] = $outUser['user_money2'] + I('post.out_money');
			$sellRes = M() -> execute('update zx_users set user_money2=user_money2-'.I('post.out_money').' where user_id='.$_SESSION['users']['user_id']);
			// $_SESSION['users']['user_money2'] = $_SESSION['users']['user_money2'] - I('post.out_money');

			// $outRes = M('users')->data(['user_money2'=>$outUser['user_money2']]) -> where('user_id = "%d "',[I('post.out_user_id')])->save();

			// $sellRes = $mod -> where('user_id = "%s" ',$_SESSION['users']['user_id'])->data(['user_money2'=>$_SESSION['users']['user_money2']])->save();
			//添加日志
				//该用户转出注册积分
				$log_money['user_money2'] = - I('post.out_money');
				$log_money['user_id'] = $_SESSION['users']['user_id'];
				$log_money['change_type'] = 50;
				$log_money['change_time'] = time();
				$log_money['change_desc'] = '您向会员'.I('post.out_user_id').'转出:$'.I('post.out_money').'注册钱包减少：$'.I('post.out_money');
				$log_money['about_user_name'] = $_SESSION['users']['user_id'];
				$logRes = $log_mod -> data($log_money)-> add();
				//收获到该用户转出的注册积分
				$log_out_money['user_money2'] =  I('post.out_money');
				$log_out_money['user_id'] = I('post.out_user_id');
				$log_out_money['change_type'] = 49;
				$log_out_money['change_time'] = time();
				$log_out_money['change_desc'] = '会员'.$_SESSION['users']['user_id'].'向您转入:$'.I('post.out_money').'注册钱包增加：$'.I('post.out_money');
				$log_out_money['about_user_name'] = $_SESSION['users']['user_id'];
				$log_out_Res = $log_mod -> data($log_out_money)-> add();
				//关联账户
				$zczhuan = M('zczhuan');
				$data['main_id'] = array('eq',$_SESSION['users']['user_id']);
				$data['user_id'] = array('eq',I('post.out_user_id'));
				$res3 = $zczhuan -> where($data)->select();

				if($res3<=0){
					$relave['main_id'] = $_SESSION['users']['user_id'];
					$relave['user_id'] = I('post.out_user_id');
					$relave['user_name'] = $mod ->where('user_id = "%s"',I('post.out_user_id'))->find()['user_name'];
					$zczhuan ->data($relave)-> add();
				}

			if($outRes>0 && $sellRes>0 && $logRes>0 && $log_out_Res>0){
				$mod -> commit();
				$this -> success('转入积分成功',U('home/switch/transfer'));
			}else{
				$mod -> rollback();
				$this -> error('转入积分失败',U('home/switch/transfer'));
			}
		}else{ 
			$mod -> rollback();
			$this -> error('抱歉您不在同一伞下',U('home/switch/transfer'));
		}

	}
	//现金积分转注册积分
	public function cashtranszc(){
		$user = M('users') -> field('user_money1') -> where('user_id=%d',[$_SESSION['users']['user_id']]) -> find();
		$this -> assign('user',$user);
		$this -> assign('title','现金积分转注册积分');
		$this -> display('');
	}

	//cash  Ajax的处理
	public function cashAjax(){
		if($_SESSION['users']['repassword'] == md5(I('post.repassword'))){
				echo '1';
		}else{
			  echo '2';
		}
	}
	//现金积分转注册积分
	public function cashTrans(){
		$cash_money = I('post.cash_num');
		$cash_mod = M('users');
		$cash_log_mod = M('account_log');
		//开启事务
		$cash_mod -> startTrans();
		$_SESSION['users']['user_money1'] = $_SESSION['users']['user_money1'] - $cash_money;
		$_SESSION['users']['user_money2'] = $_SESSION['users']['user_money2'] + $cash_money;
		$res1 = $cash_mod -> where('user_id = "%s" ',$_SESSION['users']['user_id']) -> save($_SESSION['users']);
		//封装插入日志数据
		$cash_log['user_id'] = $_SESSION['users']['user_id'];
		$cash_log['user_money1'] = -$cash_money;
		$cash_log['user_money2'] = $cash_money;
		$cash_log['change_type'] = 51;
		$cash_log['change_time'] = time();
		$cash_log['change_desc'] = '您将:$'.$cash_money.'现金币转到注册币钱包中,注册币钱包增加:$'.$cash_money;
		$cash_log['about_user_name'] = $_SESSION['users']['user_id'];
		$res2 = $cash_log_mod -> data($cash_log)->add();
		if($res1>0 && $res2>0){
			$cash_mod -> commit();
				$this -> success('转入现金积分成功',U('home/switch/cashtranszc'));
		}else{
			$cash_mod -> rollback();
				$this -> error('转入现金积分失败',U('home/switch/cashtranszc'));
		}
	}














}